import java.util.Arrays;
import java.util.Random;

public class Test {

    //将一个数组调整为所有的奇数都在所有偶数的左边[奇数 偶数]
    public static void adjust(int[] array) {
        if (array == null) {
            return;
        }
        int i = 0, j = array.length - 1;
        while (i < j) {
            while (i < j && array[i] % 2 != 0) {//左边找偶数 奇数则跳过
                i++;
            }
            while (i < j && array[j] % 2 == 0) {//右边找奇数 偶数则跳过
                j--;
            }
            swap(array,i,j);
        }
    }

    public static void main(String[] args) {
        int[] arr=new int[]{1,2,3,4,5,6,7};
        int[] arr1=new int[]{1,3,5,7};
        int[] arr2=new int[]{2,4};
        adjust(arr);
        System.out.println(Arrays.toString(arr));
        adjust(arr1);
        System.out.println(Arrays.toString(arr1));
        adjust(arr2);
        System.out.println(Arrays.toString(arr2));
    }

    public static void main6(String[] args) {
        //二维数组
        //1.定义
        int[][] arr = new int[3][];//不能省略行数
        int[][] arr2 = new int[][]{{10, 20, 30}, {11, 22, 33}};//行列必须都初始化
        int[][] arr3 = new int[3][3];

        //2.访问
        for (int i = 0; i < arr2.length; i++) {
            for (int j = 0; j < arr2[i].length; j++) {
                System.out.print(arr2[i][j] + " ");
            }
            System.out.println();
        }
        /*
         * 10 20 30
         * 11 22 33
         */

        for (int[] a : arr3) {
            for (int x : a) {
                System.out.print(x + " ");
            }
            System.out.println();
        }
        /*
         * 0 0 0
         * 0 0 0
         * 0 0 0
         */

        System.out.println(Arrays.deepToString(arr));
        //[null, null, null]

        //3.不规则数组
        int[][] irreArr = new int[3][];
        int[] Arr1 = new int[]{1, 2, 3};
        int[] Arr2 = new int[]{1, 2, 3, 4, 5, 6, 7};
        int[] Arr3 = new int[]{1};
        irreArr[0] = Arr1;
        irreArr[1] = Arr2;
        irreArr[2] = Arr3;

        for (int[] a : irreArr) {
            for (int x : a) {
                System.out.print(x + " ");
            }
            System.out.println();
        }
        /*
         * 1 2 3
         * 1 2 3 4 5 6 7
         * 1
         */
        System.out.println(Arrays.deepToString(irreArr));
        //[[1, 2, 3], [1, 2, 3, 4, 5, 6, 7], [1]]

        System.out.println();
    }

    public static void swap(int[] array, int pos1, int pos2) {
        if (array == null || pos1 >= array.length || pos2 >= array.length || pos1 < 0 || pos2 < 0) {
            return;
        }
        int tmp = array[pos1];
        array[pos1] = array[pos2];
        array[pos2] = tmp;
    }

    //冒泡排序
    public static void bubbleSort(int[] array) {
        if (array == null) {
            return;
        }
        int n = array.length;
        boolean flag;
        for (int i = 0; i < n - 1; i++) {
            flag = true;
            for (int j = 0; j < n - i - 1; j++) {
                if (array[j] > array[j + 1]) {
                    swap(array, j, j + 1);
                    flag = false;
                }
            }
            if (flag) {
                return;
            }
        }
    }

    public static void main5(String[] args) {
       /* int[] arr = {5, 2, 4, 1, 3, 10, 8, 9, 6, 7,16,13,15};
        bubbleSort(arr);
        System.out.println(Arrays.toString(arr));*/
        int[] nums = new int[20];
        Random rand = new Random();

        for (int j = 0; j < 20; j++) {
            for (int i = 0; i < 20; i++) {
                nums[i] = rand.nextInt(100) + 1;
            }
            int[] nums2 = nums.clone();
            Arrays.sort(nums);
            bubbleSort(nums2);
            if (Arrays.equals(nums, nums2)) {
                System.out.println(Arrays.toString(nums));
                System.out.println(Arrays.toString(nums2));
                System.out.println("排序正确");
            } else {
                System.out.println("排序有误");
                System.out.println(Arrays.toString(nums));
                System.out.println(Arrays.toString(nums2));
            }
        }

        /*System.out.println(Arrays.toString(nums));
        System.out.println(Arrays.toString(nums2));*/
    }

    //二分查找
    public static int binarySearch(int[] array, int key) {
        if (array == null) {
            return -1;
        }
        int left = 0, right = array.length - 1;
        while (left <= right) {
            int mid = left + ((right - left) >>> 1);
            if (array[mid] > key) {
                right = mid - 1;
            } else if (array[mid] < key) {
                left = mid + 1;
            } else {
                return mid;
            }
        }
        return -1;
    }

    public static void main4(String[] args) {
        int[] arr = {5, 2, 4, 1, 3, 10, 8, 9, 6, 7};
        System.out.println("sort before: " + Arrays.toString(arr));

        Arrays.sort(arr);//排序 默认升序
        System.out.println("sort after: " + Arrays.toString(arr));

        for (int i = 0; i <= 15; i++) {
            //System.out.print(binarySearch(arr, i)+" ");
            System.out.print(Arrays.binarySearch(arr, i) + " ");
        }
        //在调用Arrays.binarySearch 需保证数组有序(现阶段应为升序)
        //当 Arrays.binarySearch 找到key后返回其在数组中的下标 没找到返回一个负数(不一定是 -1 !!! )

        reverse(arr);
        System.out.println("\nreverse after: " + Arrays.toString(arr));

        //填充一个数组
        int[] arr2 = new int[10];
        Arrays.fill(arr2, -1);
        System.out.println(Arrays.toString(arr2));//[-1, -1, -1, -1, -1, -1, -1, -1, -1, -1]
        Arrays.fill(arr2, 0);

        //填充指定范围 [fromIndex toIndex) 不包含toIndex
        Arrays.fill(arr2, 3, 8, -1);//[0, 0, 0, -1, -1, -1, -1, -1, 0, 0]
        System.out.println(Arrays.toString(arr2));

        //比较两数组
        int[] arr3 = {5, 2, 4, 1, 3, 10, 8, 9, 6, 7};
        int[] arr4 = {5, 2, 4, 1, 3, 10, 8, 9, 6, 7};
        System.out.println(Arrays.compare(arr3, arr4));//0
        arr4[2] = 666;
        System.out.println(Arrays.compare(arr3, arr4));//-1

        //比较两数组一定范围的元素是否相同
        System.out.println(Arrays.compare(arr3, 3, 6, arr4, 3, 6));//0

        Arrays.fill(arr3, 1);
        Arrays.fill(arr4, 1);
        //判断两数组是否相等
        System.out.println(Arrays.equals(arr3, arr4));//true
        arr4[3] = 0;
        System.out.println(Arrays.equals(arr3, arr4));//false
    }

    //逆置一个数组
    public static void reverse(int[] array) {
        if (array == null) {
            return;
        }
        int left = 0, right = array.length - 1;
        while (left < right) {
            int tmp = array[left];
            array[left] = array[right];
            array[right] = tmp;
            left++;
            right--;
        }

    }

    //查找一个整型数组中的指定元素
    public static int findNum(int[] array, int x) {
        if (array == null) {
            return -1;
        }
        int pos = 0;
        for (; pos < array.length; pos++) {
            if (x == array[pos]) {
                return pos;
            }
        }
        return -1;
    }

    //计算一个整型数组的平均值
    public static double arrayAvg(int[] arr) {
        double avg = 0;
        for (int j : arr) {
            avg += j;
        }
        avg /= arr.length;
        return avg;
    }

    public static void main3(String[] args) {
        int[] arr = {1, 2, 3, 4, 5, 6};
        //System.out.println(arrayAvg(arr));//3.5
        for (int i = 0; i < 10; i++) {
            System.out.print(findNum(arr, i) + " ");
        }//-1 0 1 2 3 4 5 -1 -1 -1
        /*arr=null;
        System.out.println(findNum(arr,2));*/
    }

    public static void main2(String[] args) {
        //数组拷贝
        int[] arr = {1, 2, 3, 4, 5};
        //返回一个数组引用        被拷贝的数组名 拷贝的元素个数
        int[] arr2 = Arrays.copyOf(arr, arr.length);
        System.out.println(Arrays.toString(arr2));//[1, 2, 3, 4, 5]

        //拷贝的元素个数大于被拷贝的数组长度时 返回的数组余下元素为0
        int[] arr3 = Arrays.copyOf(arr, 2 * arr.length);
        System.out.println(Arrays.toString(arr3));//[1, 2, 3, 4, 5, 0, 0, 0, 0, 0]

        double[] dArr = {1.2, 1.3, 1.3};
        double[] dArr2 = Arrays.copyOf(dArr, 2 * dArr.length);
        System.out.println(Arrays.toString(dArr2));//[1.2, 1.3, 1.3, 0.0, 0.0, 0.0]
        //被拷贝的数组名  被拷贝的起始下标 目的地数组 目的地数组开始的下标 拷贝的长度
        System.arraycopy(arr, 0, arr2, 0, arr.length);
        System.out.println(Arrays.toString(arr2));//[1, 2, 3, 4, 5]
        //System.arraycopy 可用于部分拷贝
        int[] arr4 = new int[arr.length];

        System.arraycopy(arr, 2, arr4, 0, arr.length - 2);
        System.out.println(Arrays.toString(arr4));//[3, 4, 5, 0, 0]
       /* arr4=new int[1];
        System.arraycopy(arr,0,arr4,0,arr.length);//报错
        System.out.println(Arrays.toString(arr4));*/
        //ArrayIndexOutOfBoundsException: arraycopy: last destination index 5 out of bounds for int[1]

        //注意：
        //System.arraycopy 中的参数必须满足:
        //Object src != null && Object dest != null
        //srcPoc+length <= Object src.length
        //length <= Object dest.length

        int[] arr5 = new int[arr.length];

        //Arrays.copyOfRange(被拷贝的数组名,起始下标,拷贝的长度) 不是结束下标!
        arr5 = Arrays.copyOfRange(arr, 2, arr.length);
        System.out.println(Arrays.toString(arr5));//[3, 4, 5]

        int[] nullArr = null;
        nullArr = Arrays.copyOfRange(arr, 1, arr.length);
        System.out.println(Arrays.toString(nullArr));//[2, 3, 4, 5]
        //从上面的例子可以看出 由于 Arrays.copyOfRange 返回一个数组对象 故 = 的左边只要是一个相应的引用类型即可
        nullArr = null;

        //int[] arr6=Arrays.copyOfRange(nullArr,0,nullArr.length);//报错
        //NullPointerException: Cannot read the array length because "nullArr" is null

        //通过一个数组引用变量来生成该引用对象的一个副本
        int[] array = {1, 2, 3, 4};
        int[] array1 = array.clone();
        System.out.println(Arrays.toString(array1));//[1, 2, 3, 4]

    }

    public static void main1(String[] args) {
        int[] arr = {1, 2, 3, 4, 5};
        //int[] arr=null;
        System.out.println(myToString(arr));
    }

    //将一个整型数组转为字符串
    public static String myToString(int[] array) {
        if (array == null) {
            return "null";
        }
        String ret = "[";
        for (int i = 0; i < array.length; i++) {
            ret += array[i];
            if (i != array.length - 1) {
                ret += ",";
            }
        }
        ret += "]";
        return ret;
    }
}
